# script for downloading wikipedia data
# load with UTF-8 encoding
library(pageviews)
library(tidyverse)

# download hits for pages individually and remove unnecessary variables
AFDwiki_AfD<-article_pageviews(
  project = "de.wikipedia",
  article = "Alternative_für_Deutschland",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)
AFDwiki_AfD$views_AfD<-AFDwiki_AfD$views
AFDwiki_AfD$views<-NULL
AFDwiki_AfD$granularity<-NULL
AFDwiki_AfD$access<-NULL
AFDwiki_AfD$project<-NULL
AFDwiki_AfD$agent<-NULL
AFDwiki_AfD$language<-NULL
AFDwiki_AfD$article<-NULL



AFDwiki_gen<-article_pageviews(
  project = "de.wikipedia",
  article = "Wikipedia:Hauptseite",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)
AFDwiki_gen$views_trend<-AFDwiki_gen$views
AFDwiki_gen$views<-NULL
AFDwiki_gen$granularity<-NULL
AFDwiki_gen$access<-NULL
AFDwiki_gen$project<-NULL
AFDwiki_gen$agent<-NULL
AFDwiki_gen$language<-NULL
AFDwiki_gen$article<-NULL


wiki_Einwanderung<-article_pageviews(
  project = "de.wikipedia",
  article = "Einwanderung",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Einwanderung$views_Einwanderung<-wiki_Einwanderung$views
wiki_Einwanderung$views<-NULL
wiki_Einwanderung$granularity<-NULL
wiki_Einwanderung$access<-NULL
wiki_Einwanderung$project<-NULL
wiki_Einwanderung$agent<-NULL
wiki_Einwanderung$language<-NULL
wiki_Einwanderung$article<-NULL

wiki_Fluchtling<-article_pageviews(
  project = "de.wikipedia",
  article = "Flüchtling",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)
wiki_Fluchtling$views_Fluchtling<-wiki_Fluchtling$views
wiki_Fluchtling$views<-NULL
wiki_Fluchtling$granularity<-NULL
wiki_Fluchtling$access<-NULL
wiki_Fluchtling$project<-NULL
wiki_Fluchtling$agent<-NULL
wiki_Fluchtling$language<-NULL
wiki_Fluchtling$article<-NULL




wiki_Islam<-article_pageviews(
  project = "de.wikipedia",
  article = "Islam",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Islam$views_Islam<-wiki_Islam$views
wiki_Islam$views<-NULL
wiki_Islam$granularity<-NULL
wiki_Islam$access<-NULL
wiki_Islam$project<-NULL
wiki_Islam$agent<-NULL
wiki_Islam$language<-NULL
wiki_Islam$article<-NULL

wiki_Terrorismus<-article_pageviews(
  project = "de.wikipedia",
  article = "Terrorismus",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Terrorismus$views_Terrorismus<-wiki_Terrorismus$views
wiki_Terrorismus$views<-NULL
wiki_Terrorismus$granularity<-NULL
wiki_Terrorismus$access<-NULL
wiki_Terrorismus$project<-NULL
wiki_Terrorismus$agent<-NULL
wiki_Terrorismus$language<-NULL
wiki_Terrorismus$article<-NULL




wiki_Eurokrise<-article_pageviews(
  project = "de.wikipedia",
  article = "Eurokrise",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Eurokrise$views_Eurokrise<-wiki_Eurokrise$views
wiki_Eurokrise$views<-NULL
wiki_Eurokrise$granularity<-NULL
wiki_Eurokrise$access<-NULL
wiki_Eurokrise$project<-NULL
wiki_Eurokrise$agent<-NULL
wiki_Eurokrise$language<-NULL
wiki_Eurokrise$article<-NULL

wiki_Europaische_Union<-article_pageviews(
  project = "de.wikipedia",
  article = "Europäische_Union",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Europaische_Union$views_Europaische_Union<-wiki_Europaische_Union$views
wiki_Europaische_Union$views<-NULL
wiki_Europaische_Union$granularity<-NULL
wiki_Europaische_Union$access<-NULL
wiki_Europaische_Union$project<-NULL
wiki_Europaische_Union$agent<-NULL
wiki_Europaische_Union$language<-NULL
wiki_Europaische_Union$article<-NULL


wiki_Europaisches_Parlament<-article_pageviews(
  project = "de.wikipedia",
  article = "Europäisches_Parlament",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Europaisches_Parlament$views_Europaisches_Parlament<-wiki_Europaisches_Parlament$views
wiki_Europaisches_Parlament$views<-NULL
wiki_Europaisches_Parlament$granularity<-NULL
wiki_Europaisches_Parlament$access<-NULL
wiki_Europaisches_Parlament$project<-NULL
wiki_Europaisches_Parlament$agent<-NULL
wiki_Europaisches_Parlament$language<-NULL
wiki_Europaisches_Parlament$article<-NULL


wiki_Angela_Merkel<-article_pageviews(
  project = "de.wikipedia",
  article = "Angela_Merkel",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_Angela_Merkel$views_Angela_Merkel<-wiki_Angela_Merkel$views
wiki_Angela_Merkel$views<-NULL
wiki_Angela_Merkel$granularity<-NULL
wiki_Angela_Merkel$access<-NULL
wiki_Angela_Merkel$project<-NULL
wiki_Angela_Merkel$agent<-NULL
wiki_Angela_Merkel$language<-NULL
wiki_Angela_Merkel$article<-NULL


wiki_CDU<-article_pageviews(
  project = "de.wikipedia",
  article = "Christlich_Demokratische_Union_Deutschlands",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_CDU$views_CDU<-wiki_CDU$views
wiki_CDU$views<-NULL
wiki_CDU$granularity<-NULL
wiki_CDU$access<-NULL
wiki_CDU$project<-NULL
wiki_CDU$agent<-NULL
wiki_CDU$language<-NULL
wiki_CDU$article<-NULL

wiki_CSU<-article_pageviews(
  project = "de.wikipedia",
  article = "Christlich-Soziale_Union_in_Bayern",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_CSU$views_CSU<-wiki_CSU$views
wiki_CSU$views<-NULL
wiki_CSU$granularity<-NULL
wiki_CSU$access<-NULL
wiki_CSU$project<-NULL
wiki_CSU$agent<-NULL
wiki_CSU$language<-NULL
wiki_CSU$article<-NULL

wiki_SPD<-article_pageviews(
  project = "de.wikipedia",
  article = "Sozialdemokratische_Partei_Deutschlands",
  platform = "all",
  user_type = "user",
  start = "2015010100",
  end = "2020010100",
  reformat = TRUE,
  granularity = "daily"
)

wiki_SPD$views_SPD<-wiki_SPD$views
wiki_SPD$views<-NULL
wiki_SPD$granularity<-NULL
wiki_SPD$access<-NULL
wiki_SPD$project<-NULL
wiki_SPD$agent<-NULL
wiki_SPD$language<-NULL
wiki_SPD$article<-NULL


# left join data
Wiki_Total<-left_join(AFDwiki_gen,AFDwiki_AfD,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Angela_Merkel,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_CDU,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_CSU,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Einwanderung,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Eurokrise,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Europaische_Union,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Europaisches_Parlament,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Fluchtling,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Islam,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_SPD,by="date")
Wiki_Total<-left_join(Wiki_Total,wiki_Terrorismus,by="date")

save(Wiki_Total, file = "Wiki_Total")
